package com.lzy.cloud.job.controller;


import com.lzy.cloud.job.dtos.JobInfoDto;
import com.lzy.cloud.job.reponse.JobInfoResponse;
import com.lzy.cloud.job.request.JobInfoRequest;
import com.lzy.cloud.job.service.JobInfoService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/job")
public class JobInfoController {

    @Autowired
    private JobInfoService jobInfoService;

    /**
     * 获取所有的任务信息
     *
     * @return
     */
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public Result getAllJobs() {
        List<JobInfoDto> jobInfoDto = jobInfoService.getAllJobs();
        return new Result(DozerUtils.maps(jobInfoDto,JobInfoResponse.class));
    }

    /**
     * 增加任务
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result insertJob(@RequestBody JobInfoRequest jobInfoRequest) {
        Ensure.that(jobInfoRequest).isNotNull("000000002");
        Boolean flag = jobInfoService.insertJob(DozerUtils.map(jobInfoRequest, JobInfoDto.class));
        Ensure.that(flag).isTrue("000000003");
        return new Result(flag);
    }

    /**
     * 修改任务
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateJob(@RequestBody JobInfoRequest jobInfoRequest) {
        Ensure.that(jobInfoRequest).isNotNull("000000002");
        Boolean flag = jobInfoService.updateJob(DozerUtils.map(jobInfoRequest,JobInfoDto.class));
        Ensure.that(flag).isTrue("000000004");
        return new Result(flag);
    }

    /**
     * 删除任务
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result deleteJob(@PathVariable("id") Integer id) {
        Ensure.that(id).isNotNull("000000002");
        Boolean flag = jobInfoService.deleteJob(id);
        Ensure.that(flag).isTrue("000000005");
        return new Result(flag);
    }
}